<style scoped>
.line {
  text-align: center;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.tab-container {
  text-align: center;
  margin-bottom: 20px;
}

.app-container {
  font-size: 14px;
}

.link-type {
  color: #337ab7;
  cursor: pointer;
  text-decoration: underline;
}

.job-title {
  display: inline-block;
  height: 38px;
  line-height: 38px;
}

.job-title * {
  margin-right: 6px;
}

.job-control {
  display: inline-block;
  float: right;
  padding: 3px 0;
}

.el-card__header {
  padding: 10px 20px;
}

ul li {
  margin-bottom: 10px;
}
</style>

<template>
  <div class="app-container">
    <div class="tab-container">
      <el-button-group>
        <el-button type="primary">站外招聘</el-button>
        <el-button>原创招聘</el-button>
        <el-button>公职招聘</el-button>
      </el-button-group>
    </div>
    <el-card class="box-card">
      <div
        slot="header"
        class="clearfix"
      >
        <div class="job-title">
          <b>#{{job.oid}}</b>
          <a
            href="javascript:void(0);"
            class="link-type"
          >{{job.author}}</a>
          <a
            :href="job.link"
            target="_blank"
            class="link-type"
          >原文链接</a>
        </div>
        <div class="job-control">
          <el-button-group>
            <el-button
              size="small"
              icon="el-icon-star-on"
              @click="saveConfirm()"
            >确认并返回</el-button>
            <el-button
              size="small"
              icon="el-icon-delete"
              @click="ignore()"
            >忽略并返回</el-button>
            <el-button
              size="small"
              icon="el-icon-circle-check"
              @click="save()"
            >仅保存</el-button>
            <el-button
              size="small"
              icon="el-icon-back"
              @click="back()"
            >返回</el-button>
          </el-button-group>
        </div>
      </div>

      <el-row :gutter="20">
        <el-col :span="16">
          <div class="job-form">
            <el-form
              :inline="true"
              :model="job"
              size="small"
              class="demo-form-inline"
            >
              <el-form-item>
                <el-input
                  v-model="job.title"
                  placeholder="招聘标题"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-date-picker
                  v-model="job.cutoffTime"
                  type="datetime"
                  placeholder="招聘截止时间"
                  default-time="00:00:00"
                ></el-date-picker>
              </el-form-item>
              <el-form-item>
                <el-tag
                  size="small"
                  disable-transitions
                >{{STATUS_MAP[job.status].text}}</el-tag>
              </el-form-item>
            </el-form>
          </div>
          <tinymce-editor
            ref="editor"
            v-model="job.text"
          ></tinymce-editor>
        </el-col>
        <el-col :span="8">
          <ul>
            <li v-for="(comment,c_index) in job.comment_list" :key="c_index">
              <a
                href="javascript:void(0);"
                class="link-type"
                @click="delComment(c_index)"
              >删除</a> 
              <el-input v-model="job.comment_list[c_index]" type="textarea" autosize size="mini"></el-input>
            </li>
          </ul>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import TinymceEditor from "../../components/tinymce-editor";
import axios from "axios";

const axiosForm = axios.create({
  timeout: 1000 * 60 * 8, // request timeout
  headers: {
    "Content-Type": "application/json"
  }
});

export default {
  components: { TinymceEditor },
  data() {
    return {
      STATUS_MAP: {
        INIT: {
          text: "未审核",
          color: "#3A71A8"
        },
        IGNORE: {
          text: "忽略",
          color: "#909399"
        },
        CONFIRM: {
          text: "审核通过",
          color: "#4AB7BD"
        }
      },
      job: {
        oid: null,
        author: null,
        comment_list: [],
        createTime: null,
        detail_dom: "",
        link: null,
        cutoffTime: null,
        status: 'INIT',
        text: null,
        title: null,
        type: null
      },
      init: {
        language_url: "/tinymce/langs/zh_CN.js", // 语言包的路径
        language: "zh_CN", //语言
        skin_url: "/tinymce/skins/ui/oxide", // skin路径
        height: 300, //编辑器高度
        branding: false, //是否禁用“Powered by TinyMCE”
        menubar: false //顶部菜单栏显示
      }
    };
  },
  methods: {
    getJobDetail() {
      axiosForm
        .get("http://127.0.0.1:7001/showJobByOid", {
          params: {
            oid: this.oid
          }
        })
        .then(resp => {
          let respData = resp.data.data[0];
          this.job = respData;
          this.job.cutoffTime = new Date(respData.cutoffTime);
          this.comment_list = respData.comment_list.splice(0, 1);
        });
    },
    saveConfirm() {
      this.$confirm("是否【审核通过】该招聘信息？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.job.status = 'CONFIRM';
        axiosForm
          .post("http://127.0.0.1:7001/job/save", {
            params: this.job
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "操作成功！"
            });
            this.back();
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "操作失败！"
            });
          });
      });
    },
    ignore() {
      this.$confirm("是否【忽略】该招聘信息？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.job.status = 'IGNORE';
        axiosForm
          .post("http://127.0.0.1:7001/job/save",{
            params: this.job
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "操作成功！"
            });
            this.back();
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "操作失败！"
            });
          });
      });
    },
    save() {
      this.$confirm("是否保存该招聘信息？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        axiosForm
          .post("http://127.0.0.1:7001/job/save",{
            params: this.job
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "操作成功！"
            });
            this.getJobDetail();
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "操作失败！"
            });
          });
      });
    },
    back() {
      this.$router.push({
        path: "/"
      });
    },
    delComment(i) {
      this.$confirm("确定删除该评论？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        axiosForm
          .post("http://127.0.0.1:7001/job/save",{
            params: this.job
          })
          .then(() => {
            this.$message({
              type: "success",
              message: "操作成功！"
            });
            this.getJobDetail();
          })
          .catch(() => {
            this.$message({
              type: "error",
              message: "操作失败！"
            });
          });
      });
    }
  },
  created() {
    this.oid = this.$route.params.oid;
    this.getJobDetail();
  },
  mounted() {
    tinymce.init(this.init);
  }
};
</script>